<?php
namespace app\admin\controller;

class Advert extends Base{
    //
    protected function initialize() {
        parent::initialize();
        $this->model = 'Advert';
    }
    //
    public function same() {
        if($this->request->post('tname', '', 'trim') == 'page'){
            return json(['data'=>['error'=>'系统关键字，不能使用']]);
        }
        return parent::same();
    }
    //
    protected function _before_index(){
        $thead = [
            'aid' => 'AID',
            'xname' => '推广名称',
            'tname' => '二级目录',
            'did' => '推广域名',
            'url' => '投放链接',
            'tnid' => '正常模板',
            'tbid' => '屏蔽模板',
            'mid' => '员工名称',
            //'ismo' => '页面统计',
            'addtime' =>'创建时间',
        ];
        $this->assign('thead', $thead);
        $condition = [];
        $condition['used'] = 1;
        $memArr = model('Member')->where($condition)->field('mid,nickname')->select()->toArray();
        $this->assign('memArr', $memArr);
    }
    //
    protected function _filter($map){
        foreach($map as $key => $row){
            if($row[0] == 'did'){
                $dids = model('Domain')->where('domain', 'like', $row[2])->column('did');
                $map[$key] = ['did', 'in', $dids];
            }
        }
        return $map;
    }
    //
    protected function _after_list($list){
        $didArr = $midArr = $tnidArr = $tbidArr = [];
        foreach($list as $row){
            $didArr[] = $row['did'];
            $midArr[] = $row['mid'];
            $tnidArr[] = $row['tnid'];
            $tbidArr[] = $row['tbid'];
        }
        $arr = [
            'Domain' => ['map' => ['did' => $didArr], 'field' => 'domain', 'key' => 'did', 'var' => 'dmArr'],
            'Member' => ['map' => ['mid' => $midArr], 'field' => 'nickname', 'key' => 'mid', 'var' => 'memArr'],
            'TplNormal' => ['map' => ['tnid' => $tnidArr], 'field' => 'tname', 'key' => 'tnid', 'var' => 'tnArr'],
            'TplBlock' => ['map' => ['tbid' => $tbidArr], 'field' => 'tname', 'key' => 'tbid', 'var' => 'tbArr'],
        ];
        $info = [];
        foreach($arr as $key => $row){
            $info[$row['var']] = model($key)->where($row['map'])->column($row['field'], $row['key']);
        }
        foreach($list as $key => $row){
            $list[$key]['did'] = $info['dmArr'][$row['did']] ?? '';
            $list[$key]['url'] = $list[$key]['did'] ? $row['scheme'].'://'.$list[$key]['did'].'/'.$row['tname'] : '';
            $list[$key]['test_url'] = 'http://www.test.com'.'/'.$row['tname'];
            $list[$key]['tnid'] = $info['tnArr'][$row['tnid']] ?? '';
            $list[$key]['tbid'] = $info['tbArr'][$row['tbid']] ?? '';
            $list[$key]['mid'] = $info['memArr'][$row['mid']] ?? '';
            $list[$key]['ismo'] = $row['ismo'] == 1 ? '是' : '否';
            $list[$key]['addtime'] = date('y/m/d H:i', $row['addtime']);
        }
        return $list;
    }
    //
    protected function _before_add(){
        $dArr = model('Domain')->where('used', '=', 1)->field('did,domain,xname')->order('did', 'asc')->select()->toArray();
        $this->assign('dArr', $dArr);
        $cArr = model('Category')->where('status', '=', 1)->field('cid,cname')->order('cid', 'desc')->select()->toArray();
        $this->assign('cArr', $cArr);
        $cidArr = [];
        if($cArr){
            foreach($cArr as $row){
                $cidArr[] = $row['cid'];
            }
        }
        $pArr = model('Product')->whereIn('cid', $cidArr)->field('pid,pname,cid')->order('pid', 'asc')->select()->toArray();
        $this->assign('pArr', $pArr);
        $condition = [];
        $condition['used'] = 1;
        $mArr = model('Member')->where($condition)->field('mid,nickname')->order('mid', 'asc')->select()->toArray();
        $this->assign('mArr', $mArr);
        $baArr = model('BlockArea')->where([['use_advert', '=', 1], ['used', '=', 1]])->field('baid,area')->select()->toArray();
        $this->assign('baArr', $baArr);
        $tnArr = model('TplNormal')->where('used', '=', 1)->field('tnid,tname')->order('tnid', 'desc')->select()->toArray();
        $this->assign('tnArr', $tnArr);
        $tbArr = model('TplBlock')->where('used', '=', 1)->field('tbid,tname')->order('tbid', 'desc')->select()->toArray();
        $this->assign('tbArr', $tbArr);
        $toArr = model('TplOrder')->where('used', '=', 1)->field('toid,tname')->order('toid', 'asc')->select()->toArray();
        $this->assign('toArr', $toArr);
    }
    //
    protected function _before_insert($data){
        $data = $this->_before_save($data);
        $data['addtime'] = time();
        return $data;
    }
    //
    protected function _after_add($data, $id){
        $this->log(['pk' => 'aid', 'value' => $id], $data);
    }
    //
    protected function _before_edit(){
        $this->_before_add();
    }
    //
    protected function _filter_edit($list){
        $list['pids'] = unserialize($list['pids']);
        $list['cid'] = $list['pids'] ? model('Product')->where('pid', '=', $list['pids'][0])->value('cid') : 0;
        $list['baids'] = unserialize($list['baids']);
        return $list;
    }
    //
    protected function _before_save($data){
        $data['tname'] = str_replace(['/','*','.',','], '', trim($data['tname']));
        $data['pids'] = serialize(($data['pids'] ?? []));
        $data['baids'] = serialize(($data['baids'] ?? []));
        return $data;
    }
    //
    protected function _after_edit($data){
        $this->log(['pk' => 'aid', 'value' => $data['aid']], $data);
    }
    //
    protected function _filter_del(){
        return [1];
    }
    //
    protected function _after_del($rows){
        $logs = [];
        foreach($rows as $row){
            $logs[] = ['pk' => 'aid', 'value' => $row['aid'], 'row' => $row];
        }
        $this->logAll($logs);
    }
}